package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.BanknoteSerialNumberPairing;
import com.ruoyi.system.domain.GroupResult;
import com.ruoyi.system.domain.NiceRecord;
import com.ruoyi.system.domain.dto.BanknoteSerialNumberRecordDto;
import com.ruoyi.system.service.IBanknoteSerialNumberPairingService;
import com.ruoyi.system.service.INiceRecordService;
import com.ruoyi.system.utils.SerialNumberChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BanknoteSerialNumberRecordMapper;
import com.ruoyi.system.domain.BanknoteSerialNumberRecord;
import com.ruoyi.system.service.IBanknoteSerialNumberRecordService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.system.service.impl.UserMoneyServiceImpl.*;

/**
 * 冠字号记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class BanknoteSerialNumberRecordServiceImpl implements IBanknoteSerialNumberRecordService {
    @Autowired
    private BanknoteSerialNumberRecordMapper banknoteSerialNumberRecordMapper;
    @Autowired
    private IBanknoteSerialNumberPairingService banknoteSerialNumberPairingService;
    @Autowired
    private INiceRecordService iNiceRecordService;

    /**
     * 查询冠字号记录
     *
     * @param id 冠字号记录主键
     * @return 冠字号记录
     */
    @Override
    public BanknoteSerialNumberRecord selectBanknoteSerialNumberRecordById(Long id) {
        return banknoteSerialNumberRecordMapper.selectBanknoteSerialNumberRecordById(id);
    }

    /**
     * 查询冠字号记录列表
     *
     * @param banknoteSerialNumberRecord 冠字号记录
     * @return 冠字号记录
     */
    @Override
    public List<BanknoteSerialNumberRecord> selectBanknoteSerialNumberRecordList(BanknoteSerialNumberRecord banknoteSerialNumberRecord) {
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords = banknoteSerialNumberRecordMapper.selectBanknoteSerialNumberRecordList(banknoteSerialNumberRecord);
        if(banknoteSerialNumberRecords.size() < 3){
            List<BanknoteSerialNumberRecord> list = new ArrayList<>();


            //这里可能是单独查询的数据
            Map<Long, List<BanknoteSerialNumberRecord>> typeMap = banknoteSerialNumberRecords.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getNumberType));
            List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords2 = typeMap.get(0L);
            if (banknoteSerialNumberRecords2 != null){
                banknoteSerialNumberRecords2.forEach(item ->{
                    item.setIds(Collections.singletonList(item.getId()));
                });
                list.addAll(banknoteSerialNumberRecords2);
            }


            List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords1 = typeMap.get(1L);
            //标十
            if (banknoteSerialNumberRecords1 != null && !banknoteSerialNumberRecords1.isEmpty()) {
                banknoteSerialNumberRecords1.forEach(item ->{
                    String baseNumber = item.getSerialNumber();
                    Integer billType = item.getBillType();
                    List<String> codes = new ArrayList<>();
                    String prefix = baseNumber.substring(0, baseNumber.length() - 2);
                    for (int i = 1; i <= 10; i++) {
                        String suffix = String.format("%02d", i);
                        String newNumber = prefix + suffix;
                        codes.add(newNumber);
                    }

                    //查询数据库
                    List<BanknoteSerialNumberRecord> banknoteSerialNumberPairings = banknoteSerialNumberRecordMapper.selectByTypeAndCodes(billType, codes);
                    list.addAll(banknoteSerialNumberPairings);
                });

            }

            //标百
            List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords3 = typeMap.get(2L);
            if (banknoteSerialNumberRecords3 != null && !banknoteSerialNumberRecords3.isEmpty()) {
                banknoteSerialNumberRecords3.forEach(item ->{
                    String baseNumber = item.getSerialNumber();
                    Integer billType = item.getBillType();
                    List<String> codes = new ArrayList<>();
                    String prefix = baseNumber.substring(0, baseNumber.length() - 3);
                    int startHundreds = Integer.parseInt(baseNumber.substring(baseNumber.length() - 3, baseNumber.length() - 2));

                    for (int i = 1; i <= 100; i++) {
                        int currentValue = startHundreds * 100 + i;
                        String newNumber = prefix + String.format("%03d", currentValue % 1000);
                        codes.add(newNumber);
                    }
                    //查询数据库
                    List<BanknoteSerialNumberRecord> banknoteSerialNumberPairings = banknoteSerialNumberRecordMapper.selectByTypeAndCodes(billType, codes);
                    list.addAll(banknoteSerialNumberPairings);
                });

            }
            //连续号
            List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords4 = typeMap.get(3L);
            if (banknoteSerialNumberRecords4 != null ) {
                list.addAll(banknoteSerialNumberRecords4);
            }
            banknoteSerialNumberRecords = list;
        }


        //根据项目类别进行分类
        Map<Long, List<BanknoteSerialNumberRecord>> typeMap = banknoteSerialNumberRecords.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getNumberType));
        List<BanknoteSerialNumberRecord> list = new ArrayList<>();

        //处理单张数据
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords1 = typeMap.get(0L);
        if (banknoteSerialNumberRecords1 != null) {
            banknoteSerialNumberRecords1.forEach(item ->{
                item.setIds(Collections.singletonList(item.getId()));
            });
            list.addAll(banknoteSerialNumberRecords1);
        }

        //处理标十数据
        //按大小排序
        banknoteSerialNumberRecords.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords2 = typeMap.get(1L);
        if (banknoteSerialNumberRecords2 != null) {
            //帮我找出每个十相连的数据
            //分组
            Map<Integer, List<BanknoteSerialNumberRecord>> typeMap1 = banknoteSerialNumberRecords2.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumbers(value);
                for (GroupResult groupResult : groupResults) {
                    BanknoteSerialNumberRecord firstObject = groupResult.getFirstObject();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getList().stream().map(BanknoteSerialNumberRecord::getId).collect(Collectors.toList()));
                    list.add(firstObject);
                }
            });

        }

        //处理标百数据
        banknoteSerialNumberRecords.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords3 = typeMap.get(2L);
        if (banknoteSerialNumberRecords3 != null) {
            //分组
            Map<Integer, List<BanknoteSerialNumberRecord>> typeMap1 = banknoteSerialNumberRecords3.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumbersTens(value);
                for (GroupResult groupResult : groupResults) {
                    BanknoteSerialNumberRecord firstObject = groupResult.getFirstObject();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getList().stream().map(BanknoteSerialNumberRecord::getId).collect(Collectors.toList()));
                    list.add(firstObject);
                }
            });

        }


        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords4 = typeMap.get(3L);
        if (banknoteSerialNumberRecords4 != null) {
            //分组
            Map<Integer, List<BanknoteSerialNumberRecord>> typeMap1 = banknoteSerialNumberRecords4.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(BanknoteSerialNumberRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumberss(value);
                for (GroupResult groupResult : groupResults) {
                    BanknoteSerialNumberRecord firstObject = groupResult.getFirstObject();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getList().stream().map(BanknoteSerialNumberRecord::getId).collect(Collectors.toList()));

                    list.add(firstObject);
                }
            });

        }

        return list;
    }

    /**
     * 新增冠字号记录
     *
     * @param banknoteSerialNumberRecord 冠字号记录
     * @return 结果
     */
    @Override
    public int insertBanknoteSerialNumberRecord(BanknoteSerialNumberRecord banknoteSerialNumberRecord) {
        banknoteSerialNumberRecord.setCreateTime(DateUtils.getNowDate());
        return banknoteSerialNumberRecordMapper.insertBanknoteSerialNumberRecord(banknoteSerialNumberRecord);
    }

    /**
     * 修改冠字号记录
     *
     * @param banknoteSerialNumberRecord 冠字号记录
     * @return 结果
     */
    @Override
    public int updateBanknoteSerialNumberRecord(BanknoteSerialNumberRecord banknoteSerialNumberRecord) {
        return banknoteSerialNumberRecordMapper.updateBanknoteSerialNumberRecord(banknoteSerialNumberRecord);
    }

    /**
     * 批量删除冠字号记录
     *
     * @param ids 需要删除的冠字号记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBanknoteSerialNumberRecordByIds(Long[] ids) {
        //查询出这些号码的信息
        List<BanknoteSerialNumberRecord> banknoteSerialNumberRecords = banknoteSerialNumberRecordMapper.selectBanknoteSerialNumberRecordByIds(ids, null);
        if (banknoteSerialNumberRecords.isEmpty()){
            return 1;
        }
        //根据类别分类
        Map<Integer, List<BanknoteSerialNumberRecord>> typeMap = banknoteSerialNumberRecords.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecord::getBillType));
        //删除靓号数据
        typeMap.forEach((key, value) -> {
            List<String> collect = value.stream().map(BanknoteSerialNumberRecord::getSerialNumber).collect(Collectors.toList());
            iNiceRecordService.deleteBanknoteSerialNumberBySerialNumbers(collect,(long)key);
            //删除这些号码的配对记录
            banknoteSerialNumberPairingService.deleteByAndCodes(collect);
        });


        return banknoteSerialNumberRecordMapper.deleteBanknoteSerialNumberRecordByIds(ids);
    }

    /**
     * 删除冠字号记录信息
     *
     * @param id 冠字号记录主键
     * @return 结果
     */
    @Override
    public int deleteBanknoteSerialNumberRecordById(Long id) {
        return banknoteSerialNumberRecordMapper.deleteBanknoteSerialNumberRecordById(id);
    }

    @Transactional
    @Override
    public void insertList(List<BanknoteSerialNumberRecordDto> recordImportDtos) {
        if (recordImportDtos.isEmpty()) {
            throw new ServiceException("抱歉你导入的号码为空");
        }
        //根据类别进行分组 转换成map
        Map<Integer, List<BanknoteSerialNumberRecordDto>> map = recordImportDtos.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecordDto::getBillType));
        if (map.size() > 1) {
            throw new ServiceException("抱歉，一次只能导入一种类别的号码");
        }

        //验证这些号码是否存在
        List<String> codes = recordImportDtos.stream().map(BanknoteSerialNumberRecordDto::getSerialNumber).collect(Collectors.toList());
        List<String> collect = recordImportDtos.stream().map(BanknoteSerialNumberRecordDto::getSerialNumberOne).collect(Collectors.toList());
        List<String> collect1 = recordImportDtos.stream().map(BanknoteSerialNumberRecordDto::getSerialNumberTwo).collect(Collectors.toList());
        codes.addAll(collect);
        codes.addAll(collect1);
        BanknoteSerialNumberRecordDto banknoteSerialNumberRecordDto = recordImportDtos.get(0);
        int type = banknoteSerialNumberRecordDto.getBillType();
        List<String> codeList = new ArrayList<>();
        codes.forEach(code -> {
            if (!code.contains("J")){
                codeList.add( "J" + code);
            }else {
                codeList.add(code);
            }
        });
        List<BanknoteSerialNumberRecord> oldListTne = banknoteSerialNumberRecordMapper.selectByTypeAndCodes(type, codeList);
        if (!oldListTne.isEmpty()) {
            String msg = oldListTne.stream().map(BanknoteSerialNumberRecord::getSerialNumber).collect(Collectors.joining(","));
            throw new ServiceException("抱歉你导入的号码有存在的数据:" + msg.substring(0, 10) + "...");
        }
        for (BanknoteSerialNumberRecordDto recordImportDto : recordImportDtos) {
            recordImportDto.setCreateTime(new Date());
        }

        //根据类别进行分类
        Map<Long, List<BanknoteSerialNumberRecordDto>> typeMap = recordImportDtos.stream().collect(Collectors.groupingBy(BanknoteSerialNumberRecordDto::getNumberType));


        //单张数据
        List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos = typeMap.get(0L);

        //新增单张数据
        insertOnes(banknoteSerialNumberRecordDtos);

        //标十数据
        List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos1 = typeMap.get(1L);
        insertTen(banknoteSerialNumberRecordDtos1);


        //标百数据
        List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos2 = typeMap.get(2L);
        insertTens(banknoteSerialNumberRecordDtos2);


        //连续号数据
        List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos3 = typeMap.get(3L);
        insertContinuousCodes(banknoteSerialNumberRecordDtos3);


    }

    @Override
    public List<BanknoteSerialNumberRecord> selectByIdsAndMy(Long[] ids) {
        return banknoteSerialNumberRecordMapper.selectBanknoteSerialNumberRecordByIds(ids,SecurityUtils.getUsername());
    }


    private void insertOnes(List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos) {
        if (banknoteSerialNumberRecordDtos == null || banknoteSerialNumberRecordDtos.isEmpty()) {
            return;
        }
        List<BanknoteSerialNumberRecord> addList = new ArrayList<>();
        for (BanknoteSerialNumberRecordDto banknoteSerialNumberRecordDto : banknoteSerialNumberRecordDtos) {
            BanknoteSerialNumberRecord banknoteSerialNumberRecord = new BanknoteSerialNumberRecord(banknoteSerialNumberRecordDto);
            //验证号码是否合规
            if (!banknoteSerialNumberRecord.getSerialNumber().contains("J")){
                banknoteSerialNumberRecord.setSerialNumber("J"+banknoteSerialNumberRecord.getSerialNumber());
            }
            if (!SerialNumberChecker.isValidNumber(banknoteSerialNumberRecord.getSerialNumber())) {
                throw new ServiceException("抱歉，你输入的号码不符合规则！");
            }
            //验证是否存在相同号码
            if (banknoteSerialNumberRecordMapper.selectOneByTypeAndCode(banknoteSerialNumberRecord.getBillType(), banknoteSerialNumberRecord.getSerialNumber()) != null) {
                throw new ServiceException("抱歉，你输入的号码已存在！");
            }
//            banknoteSerialNumberRecord.setCreateTime(new Date());
//            banknoteSerialNumberRecord.setCreatorAccount(SecurityUtils.getUsername());
//            banknoteSerialNumberRecord.setNumberOwnerAccount(SecurityUtils.getUsername());
            //微信号
//            banknoteSerialNumberRecord.setContactWechatId(SecurityUtils.getLoginUser().getUser().getRemark());

            //验证是否为靓号
            if (SerialNumberChecker.isLuckyNumber(banknoteSerialNumberRecord.getSerialNumber())) {
                banknoteSerialNumberRecord.setIsNiceNumber(1L);
                // 新增一个靓号记录
                NiceRecord niceRecord = new NiceRecord(0, banknoteSerialNumberRecord.getBillType(), banknoteSerialNumberRecord.getSerialNumber(), banknoteSerialNumberRecord.getNumberType(), 1, banknoteSerialNumberRecord.getNumberOwnerAccount());
                iNiceRecordService.insertNiceRecord(niceRecord);
            }
            // 查找是否有匹配的号码
            Integer type = null;
            if (banknoteSerialNumberRecord.getBillType() == 0) {
                type = 1;
            } else {
                type = 0;
            }
            BanknoteSerialNumberRecord data = banknoteSerialNumberRecordMapper.selectOneByTypeAndCode(type, banknoteSerialNumberRecord.getSerialNumber());
            if (data != null) {
                //存在配对号码
                List<BanknoteSerialNumberPairing> list = new ArrayList<>();
                BanknoteSerialNumberPairing banknoteSerialNumberPairingOne = new BanknoteSerialNumberPairing(banknoteSerialNumberRecord.getBillType(), banknoteSerialNumberRecord.getSerialNumber(), banknoteSerialNumberRecord.getNumberOwnerAccount());
                BanknoteSerialNumberPairing banknoteSerialNumberPairingTwo = new BanknoteSerialNumberPairing(data.getBillType(), data.getSerialNumber(), data.getNumberOwnerAccount());
                list.add(banknoteSerialNumberPairingOne);
                list.add(banknoteSerialNumberPairingTwo);
                banknoteSerialNumberPairingService.insertList(list);

            }

            addList.add(banknoteSerialNumberRecord);
        }
        for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : addList) {
            banknoteSerialNumberRecord.setCreateTime(new Date());
        }
        banknoteSerialNumberRecordMapper.insertList(addList);

    }


    private void insertContinuousCodes(List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos3) {
        if (banknoteSerialNumberRecordDtos3 == null || banknoteSerialNumberRecordDtos3.isEmpty()) {
            return;
        }
        List<BanknoteSerialNumberRecord> addList = new ArrayList<>();
        for (BanknoteSerialNumberRecordDto banknoteSerialNumberRecordDto : banknoteSerialNumberRecordDtos3) {
            if (!banknoteSerialNumberRecordDto.getSerialNumberOne().contains("J")){
                banknoteSerialNumberRecordDto.setSerialNumberOne("J"+banknoteSerialNumberRecordDto.getSerialNumberOne());
            }
            if (!banknoteSerialNumberRecordDto.getSerialNumberTwo().contains("J")){
                banknoteSerialNumberRecordDto.setSerialNumberTwo("J"+banknoteSerialNumberRecordDto.getSerialNumberTwo());
            }
            List<String> codes = SerialNumberChecker.generateConsecutiveNumbers(banknoteSerialNumberRecordDto.getSerialNumberOne(), banknoteSerialNumberRecordDto.getSerialNumberTwo());
            List<BanknoteSerialNumberRecord> listItems = new ArrayList<>();
            for (String code : codes) {
                BanknoteSerialNumberRecord data = new BanknoteSerialNumberRecord(banknoteSerialNumberRecordDto);
                data.setSerialNumber(code);
                listItems.add(data);
            }

            listItems.forEach(item -> {
                if (SerialNumberChecker.isLuckyNumber(item.getSerialNumber())) {
                    item.setIsNiceNumber(1L);
                }
            });


            // 查找是否有匹配的号码
            Integer type = null;
            if (banknoteSerialNumberRecordDto.getBillType() == 0) {
                type = 1;
            } else {
                type = 0;
            }
            List<BanknoteSerialNumberRecord> dateList = banknoteSerialNumberRecordMapper.selectByTypeAndCodes(type, codes);
            //转成map
            Map<String, BanknoteSerialNumberRecord> dataMap = dateList.stream().collect(Collectors.toMap(BanknoteSerialNumberRecord::getSerialNumber, item -> item));
            List<BanknoteSerialNumberPairing> list = new ArrayList<>();
            listItems.forEach(item -> {
                BanknoteSerialNumberRecord data = dataMap.get(item.getSerialNumber());
                if (data != null) {
                    //存在配对号码
                    BanknoteSerialNumberPairing banknoteSerialNumberPairingOne = new BanknoteSerialNumberPairing(item.getBillType(), item.getSerialNumber(), item.getNumberOwnerAccount());
                    BanknoteSerialNumberPairing banknoteSerialNumberPairingTwo = new BanknoteSerialNumberPairing(data.getBillType(), data.getSerialNumber(), data.getNumberOwnerAccount());
                    list.add(banknoteSerialNumberPairingOne);
                    list.add(banknoteSerialNumberPairingTwo);
                }
            });
            if (!list.isEmpty()) {
                banknoteSerialNumberPairingService.insertList(list);
            }
            addList.addAll(listItems);
        }
        for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : addList) {
            banknoteSerialNumberRecord.setCreateTime(new Date());
        }
        banknoteSerialNumberRecordMapper.insertList(addList);
    }


    private void insertTens(List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos2) {
        List<BanknoteSerialNumberRecord> addList = new ArrayList<>();
        if (banknoteSerialNumberRecordDtos2 == null || banknoteSerialNumberRecordDtos2.isEmpty()) {
            return;
        }
        for (BanknoteSerialNumberRecordDto banknoteSerialNumberRecordDto : banknoteSerialNumberRecordDtos2) {
            String[] codes = SerialNumberChecker.checkAndGenerateSerialHundreds(banknoteSerialNumberRecordDto.getSerialNumber());
            List<BanknoteSerialNumberRecord> listItems = new ArrayList<>();
            if (!banknoteSerialNumberRecordDto.getSerialNumber().contains("J")){
                banknoteSerialNumberRecordDto.setSerialNumber("J"+banknoteSerialNumberRecordDto.getSerialNumber());
            }
            for (String code : codes) {
                BanknoteSerialNumberRecord data = new BanknoteSerialNumberRecord(banknoteSerialNumberRecordDto);
                data.setSerialNumber(code);
                listItems.add(data);
            }
            //验证是否符合靓号
            boolean luckyStandardTens = SerialNumberChecker.isLuckyStandardTens(Arrays.asList(codes));

            listItems.forEach(item -> {
                item.setIsNiceNumber(1L);

//                if (luckyStandardTens) {
//                    item.setIsNiceNumber(1L);
//                } else {
//                    item.setIsNiceNumber(0L);
//                }
//                item.setContactWechatId(SecurityUtils.getLoginUser().getUser().getRemark());
            });
            for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : listItems) {
                // 新增一个靓号记录
                NiceRecord niceRecord = new NiceRecord(0, banknoteSerialNumberRecord.getBillType(), banknoteSerialNumberRecord.getSerialNumber(), banknoteSerialNumberRecord.getNumberType(), 100, banknoteSerialNumberRecord.getNumberOwnerAccount());
                iNiceRecordService.insertNiceRecord(niceRecord);
            }
//            if (luckyStandardTens) {
//                for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : listItems) {
//                    // 新增一个靓号记录
//                    NiceRecord niceRecord = new NiceRecord(0, banknoteSerialNumberRecord.getBillType(), banknoteSerialNumberRecord.getSerialNumber(), banknoteSerialNumberRecord.getNumberType(), 100, banknoteSerialNumberRecord.getNumberOwnerAccount());
//                    iNiceRecordService.insertNiceRecord(niceRecord);
//                }
//            }

            // 查找是否有匹配的号码
            // 查找是否有匹配的号码
            Integer type = null;
            if (banknoteSerialNumberRecordDto.getBillType() == 0) {
                type = 1;
            } else {
                type = 0;
            }
            List<BanknoteSerialNumberRecord> dateList = banknoteSerialNumberRecordMapper.selectByTypeAndCodes(type, Arrays.asList(codes));
            //转成map
            Map<String, BanknoteSerialNumberRecord> dataMap = dateList.stream().collect(Collectors.toMap(BanknoteSerialNumberRecord::getSerialNumber, item -> item));
            List<BanknoteSerialNumberPairing> list = new ArrayList<>();
            listItems.forEach(item -> {
                BanknoteSerialNumberRecord data = dataMap.get(item.getSerialNumber());
                if (data != null) {
                    //存在配对号码
                    BanknoteSerialNumberPairing banknoteSerialNumberPairingOne = new BanknoteSerialNumberPairing(item.getBillType(), item.getSerialNumber(), item.getNumberOwnerAccount());
                    BanknoteSerialNumberPairing banknoteSerialNumberPairingTwo = new BanknoteSerialNumberPairing(data.getBillType(), data.getSerialNumber(), data.getNumberOwnerAccount());
                    list.add(banknoteSerialNumberPairingOne);
                    list.add(banknoteSerialNumberPairingTwo);
                }
            });
            if (!list.isEmpty()) {
                banknoteSerialNumberPairingService.insertList(list);
            }

            addList.addAll(listItems);


        }
        for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : addList) {
            banknoteSerialNumberRecord.setCreateTime(new Date());
        }
        banknoteSerialNumberRecordMapper.insertList(addList);

    }


    private void insertTen(List<BanknoteSerialNumberRecordDto> banknoteSerialNumberRecordDtos) {
        List<BanknoteSerialNumberRecord> addList = new ArrayList<>();
        if (banknoteSerialNumberRecordDtos == null || banknoteSerialNumberRecordDtos.isEmpty()) {
            return;
        }
        for (BanknoteSerialNumberRecordDto banknoteSerialNumberRecordDto : banknoteSerialNumberRecordDtos) {
            List<BanknoteSerialNumberRecord> listItems = new ArrayList<>();
            //验证号码是否合规
            if (!banknoteSerialNumberRecordDto.getSerialNumber().contains("J")){
                banknoteSerialNumberRecordDto.setSerialNumber("J"+banknoteSerialNumberRecordDto.getSerialNumber());
            }
            String[] codes = SerialNumberChecker.checkAndGenerateSerialNumbers(banknoteSerialNumberRecordDto.getSerialNumber());
            for (String code : codes) {
                BanknoteSerialNumberRecord data = new BanknoteSerialNumberRecord(banknoteSerialNumberRecordDto);
                data.setSerialNumber(code);
                listItems.add(data);
            }


            //验证这是个号码是否符合靓号规则
            boolean luckyStandardTen = SerialNumberChecker.isLuckyStandardTen(Arrays.asList(codes));


            listItems.forEach(item -> {
                item.setCreateTime(new Date());
                item.setIsNiceNumber(1L);
//                if (luckyStandardTen) {
//                    item.setIsNiceNumber(1L);
//                }
            });
            if (true) {
                for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : listItems) {
                    // 新增一个靓号记录
                    NiceRecord niceRecord = new NiceRecord(0, banknoteSerialNumberRecord.getBillType(), banknoteSerialNumberRecord.getSerialNumber(), banknoteSerialNumberRecord.getNumberType(), 10, banknoteSerialNumberRecord.getNumberOwnerAccount());
                    iNiceRecordService.insertNiceRecord(niceRecord);
                }
            }
            // 查找是否有匹配的号码
            // 查找是否有匹配的号码
            Integer type = null;
            if (banknoteSerialNumberRecordDto.getBillType() == 0) {
                type = 1;
            } else {
                type = 0;
            }
            List<BanknoteSerialNumberRecord> dateList = banknoteSerialNumberRecordMapper.selectByTypeAndCodes(type, Arrays.asList(codes));
            //转成map
            Map<String, BanknoteSerialNumberRecord> dataMap = dateList.stream().collect(Collectors.toMap(BanknoteSerialNumberRecord::getSerialNumber, item -> item));
            List<BanknoteSerialNumberPairing> list = new ArrayList<>();
            listItems.forEach(item -> {
                BanknoteSerialNumberRecord data = dataMap.get(item.getSerialNumber());
                if (data != null) {
                    //存在配对号码
                    BanknoteSerialNumberPairing banknoteSerialNumberPairingOne = new BanknoteSerialNumberPairing(item.getBillType(), item.getSerialNumber(), item.getNumberOwnerAccount());
                    BanknoteSerialNumberPairing banknoteSerialNumberPairingTwo = new BanknoteSerialNumberPairing(data.getBillType(), data.getSerialNumber(), data.getNumberOwnerAccount());
                    list.add(banknoteSerialNumberPairingOne);
                    list.add(banknoteSerialNumberPairingTwo);
                }
            });
            if (!list.isEmpty()) {
                banknoteSerialNumberPairingService.insertList(list);
            }
            addList.addAll(listItems);
        }
        for (BanknoteSerialNumberRecord banknoteSerialNumberRecord : addList) {
            banknoteSerialNumberRecord.setCreateTime(new Date());
        }
        banknoteSerialNumberRecordMapper.insertList(addList);


    }
}
